]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
tests: Use gjs_context_eval_file to evaluate test scripts
authorEvan Welsh <contact@evanwelsh.com>
Mon, 10 Jul 2023 05:24:48 +0000 (22:24 -0700)
committerEvan Welsh <contact@evanwelsh.com>
Sat, 15 Jul 2023 02:35:56 +0000 (02:35 +0000)
GJS provides utilities to run scripts given a filename, using those
utilities we can remove most of the logic implementing file loading.

Additionally the REPL logic does not make much sense for a test
runner and is unused, so remove it.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2822>

src/run-js-test.c

index ba5e875e8736b2bb1147fc252e5d1d247e53f186..b2cbd82b79671405710fd5aabf314270b6d965ce 100644 (file)
 #include "shell-global.h"
 #include "shell-global-private.h"
 
-static char *command = NULL;
-
-static GOptionEntry entries[] = {
-  { "command", 'c', 0, G_OPTION_ARG_STRING, &command, "Program passed in as a string", "COMMAND" },
-  { NULL }
-};
-
 int
 main(int argc, char **argv)
 {
@@ -50,10 +43,8 @@ main(int argc, char **argv)
   GError *error = NULL;
   ShellGlobal *global;
   GjsContext *js_context;
-  char *script;
   const char *filename;
   char *title;
-  gsize len;
   int code;
 
   context = g_option_context_new (NULL);
@@ -61,7 +52,6 @@ main(int argc, char **argv)
   /* pass unknown through to the JS script */
   g_option_context_set_ignore_unknown_options (context, TRUE);
 
-  g_option_context_add_main_entries (context, entries, NULL);
   if (!g_option_context_parse (context, &argc, &argv, &error))
     g_error ("option parsing failed: %s", error->message);
 
@@ -79,32 +69,21 @@ main(int argc, char **argv)
     exit (1);
   }
 
-  if (command != NULL) {
-    script = command;
-    len = strlen (script);
-    filename = "<command line>";
-  } else if (argc <= 1) {
-    script = g_strdup ("const Console = imports.console; Console.interact();");
-    len = strlen (script);
-    filename = "<stdin>";
-  } else /*if (argc >= 2)*/ {
-    error = NULL;
-    if (!g_file_get_contents (argv[1], &script, &len, &error)) {
-      g_printerr ("%s\n", error->message);
-      exit (1);
-    }
-    filename = argv[1];
+  if (argc < 2) {
+    g_printerr ("Missing filename");
+    exit(1);
   }
 
+  filename = argv[1];
   title = g_filename_display_basename (filename);
   g_set_prgname (title);
   g_free (title);
 
-  /* evaluate the script */
   error = NULL;
-  if (!gjs_context_eval (js_context, script, len,
-                         filename, &code, &error)) {
-    g_free (script);
+
+  /* evaluate the script */
+  bool success = gjs_context_eval_file(js_context, filename, &code, &error);
+  if (!success) {
     g_printerr ("%s\n", error->message);
     exit (1);
   }
@@ -113,6 +92,5 @@ main(int argc, char **argv)
   gjs_context_gc (js_context);
 
   g_object_unref (js_context);
-  g_free (script);
   exit (code);
 }